home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 41.zip
/
BS1 part 41
/
Compute`s Amiga resource 1.adf
/
Source
/
Mr.Gadget
/
mrgadget.c
Wrap
C/C++ Source or Header
|
1989-02-07
|
14KB
|
472 lines
/*
MrGadget
Copyright 1989 COMPUTE! Publications, Inc.
All Rights Reserved
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <intuition/intuition.h>
#include <exec/exec.h>
#ifdef SKIP
This set of functions was created for saving time in program creation.
It will open a window with the requested string gadgets and then the requested
Boolean gadgets. As many MrGadget windows can be opened at a time as needed as
long as a each window has it's own MrGadget structure.
A number of cautions are to be taken if this system is used: Do not request
any gadgets with titles longer than 70 characters or any string gadgets
that accept more than 70 characters. Do not request more gadgets than will fit
in the window vertically. The window is 200 pixels tall. String gadgets are 26
pixels tall and boolean gadgets are 20 pixels tall. The first gadget starts 36
pixels down from the top (we leave room for a message), so you have 164 pixels
maximum (200 - 36 = 164) to work with.
164 / 26 = 6 string gadgets in the window at one time
164 / 20 = 8 boolean gadget in the window at one time
or a mixture of both.
#endif
/* menu bar for Yes or No window */
struct IntuiText MrGadMenuT1 = {3,1,COMPLEMENT,0,0,NULL," NO",NULL};
struct MenuItem MrGadItem2 =
{NULL,0,9,96,8,ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP,0,
(APTR)&MrGadMenuT1,NULL,'N',NULL,MENUNULL};
struct IntuiText MrGadMenuT2 = {3,1,COMPLEMENT,0,0,NULL,"YES",NULL};
struct MenuItem MrGadItem1 =
{&MrGadItem2,0,0,96,8,ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP,0,
(APTR)&MrGadMenuT2,NULL,'Y',NULL,MENUNULL};
struct Menu MrGadMenu = {NULL,0,0,93,0,MENUENABLED,"Yes or No",&MrGadItem1};
/* special structure to keep track of mrgadget and to open it with */
struct MrGadget {
char *title; /* title to be given to window structure */
int ns; /* number of string gadgets */
int nb; /* number of boolean gadgets */
char **strtext; /* pointer to pointer to title of each string
* and boolean gadget, string will be stored first
* then boolean */
char **getstr; /* pointer to pointer to storage for each string
* gadget. getstr will be sent with a null term. string
* that is the size of the buffer such as 32 char input
* you will send a 32 char null terminated string */
/* this rest of this structure is maintained by the functions of the
* program. they are used for memory allocations and pointers
* that the functions require. do not tamper with */
struct Window *w;
struct RastPort *rp;
struct Gadget *gad; /* memory for gad structures */
struct IntuiText *itext; /* memory for Text structures */
struct Border *border; /* memory for borders */
struct StringInfo *stri; /* memory for string info */
SHORT *pairs; /* memory for pairs */
};
struct NewWindow mrgadgetnw = {
0,0,0,0,1,2,
GADGETUP | MENUPICK, /* nw.IDCMPFlags */
ACTIVATE | WINDOWDRAG, /* nw.flags */
NULL,NULL,NULL,NULL,NULL,
70,20,640,200,
WBENCHSCREEN
};
/* generic structures use though initialize the gad. structures */
UBYTE UNDOBUFFER[80]; /* same undo buffer for all strings */
struct StringInfo GenStrINFO =
{NULL,UNDOBUFFER,0,20,0,0,0,0,0,0,0,0,NULL};
struct Border GenBorder = {-2,-2,2,0,JAM2,5,NULL,NULL};
struct IntuiText GenText = {3,0,JAM2,0,-12,NULL,NULL,NULL};
struct Gadget GenGadget = {NULL,0,0,0,8,GADGHCOMP,RELVERIFY,
STRGADGET,(APTR)&GenBorder,NULL,&GenText,NULL,NULL,0,NULL};
void
closeMrGadget(mrgad)
struct MrGadget *mrgad;
{
int i;
i = mrgad->ns + mrgad->nb;
if(mrgad->w) CloseWindow(mrgad->w);
if(mrgad->gad) FreeMem(mrgad->gad,sizeof(struct Gadget)*i);
if(mrgad->itext) FreeMem(mrgad->itext,sizeof(struct IntuiText)*i);
if(mrgad->border) FreeMem(mrgad->border,sizeof(struct Border) * i * 2);
if(mrgad->stri) FreeMem(mrgad->stri,sizeof(struct StringInfo)* mrgad->ns);
if(mrgad->pairs) FreeMem(mrgad->pairs,sizeof(SHORT) * i * 10 * 2);
}
int /* true or false */
openMrGadget(mrgad)
struct MrGadget *mrgad;
{
int i, j; /* loop counters */
int windw;
int top;
int width;
int dostr;
int gadid;
char **getptr, **textptr;
struct Gadget *gad;
struct IntuiText *itext;
struct Border *border;
struct StringInfo *stri;
SHORT *pairs;
mrgad->w = NULL; /* window is not open yet */
mrgad->rp = NULL;
/* get memory for the gadgets stuff */
i = mrgad->ns + mrgad->nb; /* total the number of gadgets needed */
mrgad->gad=(struct Gadget *)
AllocMem(sizeof(struct Gadget) * i,0);
mrgad->itext=(struct IntuiText *)
AllocMem(sizeof(struct IntuiText) * i,0);
mrgad->border=(struct Border *)
AllocMem(sizeof(struct Border) * i * 2,0);
if(mrgad->ns)
mrgad->stri=(struct StringInfo *)
AllocMem(sizeof(struct StringInfo) * mrgad->ns,0);
mrgad->pairs = (SHORT *)
AllocMem(sizeof(SHORT) * i * 10 * 2,0);
if(mrgad->ns != 0 && ! mrgad->stri) {
closeMrGadget();
return(FALSE); /* failed let caller know */
}
if(! mrgad->gad || ! mrgad->itext || ! mrgad->border ||
! mrgad->pairs ) {
closeMrGadget();
return(FALSE); /* failed let caller know */
}
/* we have the memory now set up the gadgets */
/* copy all generic information to all of the structures */
*mrgad->gad = GenGadget;
*mrgad->itext = GenText;
*mrgad->border = GenBorder;
if(mrgad->ns) *mrgad->stri = GenStrINFO; /* be sure you need stri */
gad = mrgad->gad; /* first of gad memory */
itext = mrgad->itext;
border = mrgad->border;
j = mrgad->ns + mrgad->nb;
*(border + j) = *mrgad->border; /* wont be copied below */
for(i=1;i < j ;i++) {
gad++, itext++, border++; /* move all to next one */
*gad = *mrgad->gad;
*itext = *mrgad->itext;
*border = *(border + j) = *mrgad->border;
}
stri = mrgad->stri;
for(i=1;i<mrgad->ns;i++) {
stri++;
*stri = *mrgad->stri;
}
gad = mrgad->gad; /* first of gad memory */
itext = mrgad->itext;
border = mrgad->border;
pairs = mrgad->pairs;
stri = mrgad->stri;
getptr = mrgad->getstr;
textptr = mrgad->strtext;
gadid = 0; /* all string gadget will be supplied with 0 */
/* will make the window width at least as wide as the title */
windw = (strlen(mrgad->title)* 8)+16;
top = 34; /* first gadget starts 34 pix down */
for(i=0;i<mrgad->ns + mrgad->nb ;i++) {
if(i == mrgad->ns && mrgad->nb) top -= 6; /* space for bool */
if(i<mrgad->ns) dostr = TRUE; /* first build strings */
else dostr = FALSE; /* then build bool gadgets */
if(dostr) width = strlen(*getptr)+1; /* null tells how big */
else width = strlen(*textptr);
width *= 8; /* each char 8 wide */
if(! dostr) width += 16; /* for BOOL text */
else width += 8; /* for string gadget */
gad->Width = width-2;
if(windw < width) windw = width;
for(j=0;j<10;j++) {
if(dostr) {
*(pairs + j) = -3; /* outside square */
*(pairs + j + 10) = 0; /* inside square */
}
else {
*(pairs + j) = 0; /* outside square */
*(pairs + j + 10) = 3; /* inside square */
}
} /* end for */
if(dostr) {
/* pairs X quad. */
*(pairs + 2) = *(pairs + 4) = width;
*(pairs + 2 + 10) = *(pairs + 4 + 10) = width - 3;
/* pairs Y quad. */
*(pairs + 5) = *(pairs + 7) = 14;
*(pairs + 5 + 10) = *(pairs + 7 + 10) = 11;
}
else {
/* pairs X quad. */
*(pairs + 2) = *(pairs + 4) = width;
*(pairs + 2 + 10) = *(pairs + 4 + 10) = width - 3;
/* pairs Y quad. */
*(pairs + 5) = *(pairs + 7) = 16;
*(pairs + 5 + 10) = *(pairs + 7 + 10) = 13;
}
border-> XY = pairs;
border-> NextBorder = (border + 1);
gad->GadgetRender = (APTR) border;
border++; /* next border for small square */
pairs += 10; /* move to small pairs */
border->XY = pairs;
border->FrontPen = 3;
border++;
pairs += 10;
j = width; /* still hold width of get string */
width = strlen(*textptr);
width *= 8; /* char 8 pix wide */
if(windw < width) windw = width;
if(dostr){
itext->TopEdge = -12; /* print text above gadget */
itext->LeftEdge = (j - width) / 2; /* center text */
}
else {
itext->TopEdge = 3;
itext->LeftEdge = 8;
}
itext->IText = *textptr;
gad->GadgetText = itext;
itext++;
if(dostr) {
stri->MaxChars = strlen(*getptr)+1; /*count term.NULL*/
stri->Buffer = *getptr;
**getptr = '\0'; /* null string */
gad->SpecialInfo = (APTR) stri;
stri++;
}
else gad->Height = 13;
gad->NextGadget = (gad + 1);
gad->LeftEdge = 4; /* should be changed later onto center*/
gad->TopEdge = top;
if(dostr) {
gad->GadgetType = STRGADGET;
getptr++;
top +=26; /* move past to the next gadget */
}
else {
gad->GadgetType = BOOLGADGET;
gadid++;
top +=20; /* move past to the next gadget */
}
gad->GadgetID = gadid; /* all string gadgets get 0 bool 1-2-3-- */
textptr++;
gad++;
}
gad--; /* back up a gadget need to null the last NextGadget */
gad->NextGadget = NULL;
gad = mrgad->gad;
windw += 40; /* give 20 pix a side of widest gadget */
for(i=0;i<mrgad->ns + mrgad->nb ;i++) {
gad->LeftEdge = (windw - gad->Width)/2;
gad++;
}
mrgadgetnw.FirstGadget = mrgad->gad;
mrgadgetnw.Width = windw;
mrgadgetnw.Height = top;
mrgadgetnw.Title = mrgad->title;
/* open the window */
if((mrgad->w = (struct Window *)OpenWindow(&mrgadgetnw))== NULL) {
closeMrGadget();
return(FALSE);
}
mrgad->rp = mrgad->w->RPort;
SetDrMd(mrgad->rp,JAM2);
SetAPen(mrgad->rp,1); /* pen = BLACK */
SetBPen(mrgad->rp,0); /* background = WHITE */
return(TRUE);
}
int
useMrGadget(mrgad,message)
struct MrGadget *mrgad;
char *message; /* a message to be displayed if needed */
{
struct IntuiMessage *mes;
int class, code;
int gadid; /* gadget ID for return value */
struct Gadget *gad;
int x, y;
char *space;
space = " ";
x = mrgad->w->Width / 8; /* number of char across window */
y = mrgad->w->BorderTop + 8;
/* clear any possible message at the top of the window */
Move(mrgad->rp,mrgad->w->BorderLeft,y);
Text(mrgad->rp,space,x - 1);
RefreshGadgets(mrgad->gad,mrgad->w,NULL);
if(*message) {
y = mrgad->w->BorderTop + 8;
x = mrgad->w->Width - (strlen(message)*8);
if(x) x /= 2; /* be sure not to divide by 0 */
if(x >= 0) {
Move(mrgad->rp,x,y);
Text(mrgad->rp,message,strlen(message));
}
}
for(;;)
{
if((mes = (struct IntuiMessage *)
GetMsg(mrgad->w->UserPort)) == 0L) {
Wait(1L<<mrgad->w->UserPort->mp_SigBit);
continue;
} /* end if */
class = mes->Class;
code = mes->Code;
ReplyMsg(mes);
gadid = -1;
switch (class){
case MENUPICK :
if(code != MENUNULL){
if(MENUNUM(code) == 0) gadid = ITEMNUM(code)+1;
break;
}
case GADGETUP :
gad = (struct Gadget *) mes->IAddress;
gadid = gad->GadgetID;
break;
} /* switch */
if(gadid != -1) break;
} /* for */
return(gadid);
}
int /* TRUE OR FALSE */
YesNo(question)
char *question;
{
struct MrGadget mrgad;
char *strtext[2], *message;
int reply, flag;
message = " ";
strtext[0] = "YES";
strtext[1] = "NO";
mrgad.title = question;
mrgad.ns = 0; /* no string gadgets */
mrgad.nb = 2; /* 2 bool gadgets */
mrgad.strtext = strtext;
mrgad.getstr = NULL;
flag = openMrGadget(&mrgad);
if(! flag) {
return(-1);
}
SetMenuStrip(mrgad.w,&MrGadMenu); /* add the menu */
for(;;) {
flag = FALSE;
reply = useMrGadget(&mrgad,message);
switch (reply) {
case 1: /* YES gadget hit */
reply = flag = TRUE;
break;
case 2: /* NO gadget hit */
flag = TRUE;
reply = FALSE;
break;
} /* end switch */
if(flag) break;
} /* end for */
if(mrgad.w) ClearMenuStrip(mrgad.w);
closeMrGadget(&mrgad);
return(reply);
}
/*==================== end of MrGadget functions =======================*/
/* The above functions fit nicely in to a small library to link from */
/*======================================================================*/
/*====== All following code is to demonstrate the use of MrGadget ======*/
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
void
Close_shop()
{
if (GfxBase) CloseLibrary(GfxBase);
if (IntuitionBase) CloseLibrary(IntuitionBase);
}
void
Startup()
{ /* Open the intuition library. */
IntuitionBase = (struct IntuitionBase *)
OpenLibrary("intuition.library",0);
GfxBase = (struct GfxBase *)
OpenLibrary("graphics.library",0);
if(! IntuitionBase || ! GfxBase) {
/* bad news one or both Libraries did not open */
Close_shop();
exit(FALSE);
}
}
void /* Simple display of 4 string gadgets and 2 BOOL gadgets */
main()
{
struct MrGadget mrgad;
char *strtext[6], *getstr[4], *ptr;
int numstring, numbool, len, i, j, good, code;
char *message;
Startup();
numstring = 4;
numbool = 2;
strtext[0] = "string gadget #1";
strtext[1] = "string gadget #2";
strtext[2] = "string gadget #3";
strtext[3] = "string gadget #4";
strtext[4] = "Yes or No mrgadget";
strtext[5] = "bool gadget #2";
getstr[0] = " ";
getstr[1] = " ";
getstr[2] = " ";
getstr[3] = " ";
len = 5;
for(j=0;j<numstring;j++) { /* build different size getstr buffers */
ptr = getstr[j];
for(i=0;i<len * (j+1);i++){
*ptr = ' ';
ptr++;
} /* end for i */
*ptr='\0'; /* each one is filled to size, then ended in NULL */
} /* end for j */
mrgad.title = "MrGadget in Action!";
mrgad.ns = numstring;
mrgad.nb = numbool;
mrgad.strtext = strtext;
mrgad.getstr = getstr;
code = openMrGadget(&mrgad);
if(! code) {
printf("MrGadget failed\n");
Close_shop();
exit(0);
}
message = "hello world"; /* send a message to start with */
for(;;) {
good = FALSE;
code = useMrGadget(&mrgad,message);
switch (code) {
case 0: /* user returned on string gadget */
for(i=0;i<numstring;i++) /* display them */
printf("%s\n",getstr[i]);
message = "Returned on a string";
break;
case 1: /* first gadget is equal to 1 */
printf("bool gadget #1\n");
message = "Hit BOOL gadget 1";
good = YesNo("Quit this time?");
break;
case 2: /* second gadet is equal to 2 */
printf("bool gadget #2\n");
message = "Hit BOOL gadget 2";
break;
} /* end switch */
printf ("value returned from useMrGadget = %d\n",code);
if(good) break; /* good is changed by the YesNo function */
} /* end endless for */
closeMrGadget(&mrgad);
Close_shop();
}